<?php

namespace App\Library\Logic;

use App\Library\Requests\Kefu\KefuPageRequest;
use App\Models\KefuModel;

class KefuLogic extends KefuModel
{
    /**
     * paging
     * @param KefuPageRequest $request
     * @param int $sellerId
     * @return mixed
     */
    public function paging(KefuPageRequest $request, int $sellerId): mixed
    {
        $builder = self::query()->where('status', 1)->where('seller_id', $sellerId)->where('kefu.user_id', '<>', USERID);
        $like = strip_tags(trim($request->name));
        if (!empty($like)) $builder->where('name', 'like', "%$like%");
        return $builder->get([
            'user_id',
            'name',
            'file_id',
        ]);
    }

    /**
     * changeStatus
     * @param int $status
     * @return mixed
     */
    public static function changeStatus(int $status): int
    {
        return self::query()->where('user_id', USERID)->where('status', '<>', $status)->update(['status' => $status]);
    }

    /**
     * online
     * @param int $sellerId
     * @return mixed
     */
    public static function online(int $sellerId): array
    {
        return self::query()->where('status', 1)->where('seller_id', $sellerId)
            ->pluck('user_id')->toArray();
    }

    public static function isKeFu(int $userId): ?int
    {
        return KefuLogic::query()->where('user_id', $userId)->orWhere('seller_id', $userId)->value('seller_id');
    }
}
